<?php

class Validator
{
    public static function Validate($value, $type = 'require', $requreArr = array(), $check_password_format = true)
    {
        if ($type == 'number') {
            return true;
        } elseif ($type == 'password' && $check_password_format == true) {
            $pattern = '/[A-Za-z0-9]{6,18}/';
            if (!preg_match($pattern, $value)) {
                return false;
            }
            if (strlen($value) > 18) {
                return false;
            }
            return true;
        } elseif ($type == 'url') {
            $pattern = '/^http[s]?:\/\/' .
                '(([0-9]{1,3}\.){3}[0-9]{1,3}' . // IP形式的URL- 199.194.52.184
                '|' . // 允许IP和DOMAIN（域名）
                '([0-9a-z_!~*\'()-]+\.)*' . // 域名- www.
                '([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.' . // 二级域名
                '[a-z]{2,6})' .  // first level domain- .com or .museum
                '(:[0-9]{1,4})?' .  // 端口- :80
                '((\/\?)|' .  // a slash isn't required if there is no file name
                '(\/[0-9a-zA-Z_!~\'\(\)\[\]\.;\?:@&=\+\$,%#-\/^\*\|]*)?)$/';
            if (preg_match($pattern, $value)) {
                return true;
            }
            return false;
        } elseif ($type == 'email') {
            if (preg_match('/^[a-z0-9]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?$/', $value)) {
                return true;
            }
            return false;
        } elseif ($type == 'username') {
            $pattern = '/[^A-Za-z0-9\x7f-\xff]+/';
            if (preg_match($pattern, $value)) {
                return false;
            }
            if (strlen($value) < 4 || strlen($value) > 16) {
                return false;
            }
            return true;

        } elseif ($type == 'scureID') {
            $value = strtoupper($value);
            $length = strlen($value);
            //判断位数,只支持18位
            if ($length != 18) {
                return false;
            }
            //验证是否是数字和X
            $sublength = $length - 1;
            $pattern = '/[0-9]{' . $sublength . '}[0-9X]/';
            $match = preg_match($pattern, $value);
            if (!$match) {
                return false;
            }
            //验证加权
            $year = substr($value, 6, 2);
            if ($year < 19 || $year > 20) {
                return false;
            }
            $w = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
            $y = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
            $sum = 0;
            for ($i = 0; $i < $length - 1; $i++) {
                $sum += substr($value, $i, 1) * $w[$i];
            }
            $last = $sum % 11;
            if (substr($value, 17, 1) != $y[$last]) {
                return false;
            }
            return true;

        } elseif ($type == 'mobile') {
            $rule = '/^1[3|4|5|6|7|8|9][\d]{9}/';
            preg_match($rule, $value, $regex);
            if ((isset($regex[0]) && !$regex[0]) || strlen($value) != 11) {
                return false;
            }
            return true;
        } elseif ($type == 'adult') {
            if (strlen($value) != 18) {
                return false;
            }
            //校验身份证号年龄
            $birth_date = substr($value, 10, 4);
            $birth_year = substr($value, 6, 4);
            $adult_year = $birth_year + 18;
            $adult_date = $adult_year . $birth_date;
            if (intval($adult_date) < intval(date('Ymd'))) {
                return true;
            } else {
                return false;
            }
        } elseif ($type == 'encryp') {
            $res = UserLoginKey::getKey($value);
            if ($res == null) {
                return false;
            }
            return $res;
        } else  //require
        {
            if (is_array($value)) {
                foreach ($requreArr as $val) {
                    if (!isset($value[$val]) || empty($value[$val])) {
                        return false;
                    }
                }
                return true;
            } else {
                $res = empty($value) ? false : true;
                return $res;
            }
        }
    }

    public static function fixlink($link, $https = null)
    {
        $link = trim(($link));
        if ($https) {
            $prefix = 'https://';
        } else {
            $prefix = 'http://';
        }
        if (strpos($link, 'http') === false) {
            return $prefix . $link;
        } else {
            return $link;
        }
    }

    public static function checkData($data, $fields)
    {
        $check = true;
        foreach ($fields as $val) {
            if (!isset($data[$val])) {
                $check = false;
                break;
            }
            $data[$val] = trim($data[$val]);
        }
        return $check;
    }

    /**
     * 验证交易密码格式
     * @param $password
     * @return bool
     */
    public static function checkPayPwd($password)
    {
        if (strlen($password) > 6) {
            return false;
        }
        if (preg_match("/[0-9]{6}/", $password)) {
            return true;
        }
        return false;

    }
}

?>